/*
 * NewClass.java
 *
 * Created on Aug 19, 2007, 3:01:59 PM
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oslo.data.dummy;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.oslo.data.DataException;
import org.oslo.data.DataProvider;
import org.oslo.domain.HotelInfo;
import org.oslo.domain.ImageInfo;
import org.oslo.domain.LocationInfo;

/**
 *Long/lat
 * 
 * @author nl24167
 */
public class DummyDataProvider implements DataProvider {

    private HashMap<String, LocationInfo> locations = new HashMap<String, LocationInfo>();
    private HashMap<String, List<HotelInfo>> hotels = new HashMap<String, List<HotelInfo>>();
    private final String PHOTO_LOCATION = "testResources/photos/";

    public DummyDataProvider() {
        // setup dummy locations
        locations.put("JavaOne", new LocationInfo(-122.40082740783691d, 37.78333309725631d));
        locations.put("JavaZone", new LocationInfo(-122.40082740783691d, 37.78333309725631d));
        
        // setup dummy hotels
        HotelInfo jo1 = new HotelInfo(40f, 90f, 3.2f, "Hotel 1", "Some street1", "Some city1", -122.40182740783691d, 37.78233309725631d);
        HotelInfo jo2 = new HotelInfo(50f, 80f, 4.5f, "Hotel 2", "Some street2", "Some city2", -122.40282740783691d, 37.78533309725631d);
        HotelInfo jo3 = new HotelInfo(60f, 80f, 1.3f, "Hotel 3", "Some street3", "Some city3", -122.40382740783691d, 37.78333309725631d);

        HotelInfo jz1 = new HotelInfo(40f, 90f, 3.2f, "Hotel 1", "Some street1", "Some city1", 50.22d, -5.3d);
        HotelInfo jz2 = new HotelInfo(50f, 80f, 4.5f, "Hotel 2", "Some street2", "Some city2", 50.23d, -5.4d);
        HotelInfo jz3 = new HotelInfo(60f, 80f, 1.3f, "Hotel 3", "Some street3", "Some city3", 50.24d, -5.5d);

        ArrayList<HotelInfo> joList = new ArrayList<HotelInfo>();
        joList.add(jo1);joList.add(jo2);joList.add(jo3);        
        ArrayList<HotelInfo> jzList = new ArrayList<HotelInfo>();
        jzList.add(jz1);jzList.add(jz2);jzList.add(jz3);        
        
        hotels.put("JavaOne", joList);
        hotels.put("JavaZone", jzList);
    }

    public LocationInfo getConferenceLocation(String conferenceName, String conferenceLocation) throws DataException {
        return locations.get(conferenceName);
    }

    public List<ImageInfo> getSessionImages(String conferenceName, String conferenceLocation) throws DataException {
        File[] fileList = new File(PHOTO_LOCATION + conferenceName).listFiles();
        ArrayList<ImageInfo> result = new ArrayList<ImageInfo>();
        try {
            for (File imageFile : fileList) {
                ImageInfo info = new ImageInfo(imageFile.getName(), getFileData(imageFile));
                result.add(info);
            }
        } catch (Exception exception) {
            throw new DataException("Error retrieving images",exception);
        }
        
        return result;
    }
    
   public List<HotelInfo> getHotels(String conferenceName, String conferenceLocation) throws DataException {
        return hotels.get(conferenceName);
    }
   
    public void init() {
        // noop
    }   

    private byte[] getFileData(File file) throws IOException {
        FileInputStream fIn = new FileInputStream(file);
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        int buffer = 4096;
        byte[] data = new byte[buffer];
        int read = fIn.read(data);

        while (read != -1) {
            bOut.write(data, 0, read);
            read = fIn.read(data);
        }

        return bOut.toByteArray();
    }
}
